const util = require('../../utils/util.js')
Page({
  data: {
    todayList: [],
    noInfo: '',
    currentTime: ''
  },

  onLoad: function() {
    this.setData({
      currentTime: util.time()
    })
    if(!wx.getStorageSync('openid')) {
      this.getOpenid()
    } else {
      this.getTodayList()
    }
  },

  getOpenid: function() {
    var that = this
    wx.cloud.callFunction({
      name: 'getOpenid',
      success: function(res) {
        wx.setStorageSync('openid', res.result)
        that.getTodayList()
      }
    })
  },

  getTodayList: function() {
    var that = this
    wx.cloud.callFunction({
      name: 'getTodayList',
      data: {
        examDate: util.date(),
        openid: wx.getStorageSync('openid')
      },
      success: function(res) {
        wx.stopPullDownRefresh()
        that.setData({
          todayList: res.result
        })
        if(res.result.length == 0) {
          that.setData({
            noInfo: '今日暂无考试任务'
          })
        }
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },

  manageExam: function() {
    wx.navigateTo({
      url: '../login/login',
    })
  },
  task: function() {
    wx.navigateTo({
      url: '../task/task',
    })
  },
  onPullDownRefresh: function() {
    this.getTodayList()
  },
  onShareAppMessage: function(e) {
    return {
      title: '考务小贝 - 随时关注考试信息'
    }
  },
  onShareTimeline: function() {
    return {
      title: '考务小贝 - 随时关注考试信息'
    }
  }
})